package com.xt.website.controller;

import com.xt.website.po.Category;
import com.xt.website.po.CategoryExt;
import com.xt.website.po.CategorySecond;
import com.xt.website.service.CategoryService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.util.ArrayList;
import java.util.List;

/*
 * @Author 爱尚实训赵国鑫
 * @ClassName  CategoryController
 * @Date 2019/8/18/10:13
 * @Description TODO:(一句话描述这个类)
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;
    @Resource
    private ServletContext application;

    /**
     * 获取全部一二级类目
     *
     * @return
     */
    @RequestMapping("/findAllCategories")
    public ArrayList<CategoryExt<CategorySecond>> findAllCategories() {
        ArrayList<CategoryExt<CategorySecond>> allCategories = (ArrayList<CategoryExt<CategorySecond>>) application.getAttribute("allCategories");

        if (allCategories == null) {
            allCategories = categoryService.getAllCategories();
            application.setAttribute("allCategories", allCategories);
        }
        return allCategories;
    }

    /**
     * 获取全部一级类目
     */
    @RequestMapping("/findCategories")
    public List<Category> findCategories() {
        return categoryService.getCategories();
    }

    /**
     * 根据一级类目id获取全部二级类目
     */
    @RequestMapping("/findCategorySecondByCid")
    public List<CategorySecond> findCategorySecondByCid(Integer cid) {
        return categoryService.findCategorySecondByCid(cid);
    }

}
